package com.boyuan.huijiaoyu;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;

import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;
import com.ttcaca.cas.android.client.utils.Helper;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class UploadFileActivity extends BaseActivity implements OnClickListener{
	
	private static final int FILE_SELECT_CODE = 1014;
	private String caseID;
	private String id;
	private String realname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_upload_file);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		id = sp.getString("id", "");
		realname = sp.getString("realname", "");
		Intent intent = getIntent();
		caseID = intent.getStringExtra("caseID");
		showFileChooser();
		
	}
	
	private void showFileChooser() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("*/*");
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		try {
			startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"), FILE_SELECT_CODE);
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(UploadFileActivity.this, "请安装文件管理器", Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == FILE_SELECT_CODE && resultCode == Activity.RESULT_OK){
			Uri uri = data.getData();
			String file_path = Helper.getFileAbsolutePath(UploadFileActivity.this, uri);
			String file_name = file_path.substring(file_path.lastIndexOf("/") + 1, file_path.length());
			File file = new File(file_path);
			if(!file.exists()){
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			hideLoading();
			Helper.uploadFile(Constant.HttpUrl + ":4200/v2/files?corp_id=1&", caseID, file_name, id, realname, file, new PostRecordResponseListener() {
				
				@Override
				public void onFinish(String succString) {
					Intent intent = new Intent();
					intent.setAction(Constant.UPLOADFILE);
					sendBroadcast(intent);
					Toast.makeText(UploadFileActivity.this, succString, Toast.LENGTH_SHORT).show();
					finish();
				}
				
				@Override
				public void onFailed(String failString) {
					hideLoading();
					Toast.makeText(UploadFileActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
					finish();
				}
				
			});
			Helper.showToast(UploadFileActivity.this, "后台上传中！");
		}else{
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
		
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.upload_file, menu);
		return true;
	}

	@Override
	public void onClick(DialogInterface arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void backClicked() {
		if (isShowingLoading()) {
			hideLoading();
		} else {
			setResult(RESULT_OK);
			isBackAllowed = true;
		}
	}

	

}
